// suserdb
package swdb

import (
	"database/sql"
	"time"
	//	"fmt"
	//	"io"
	"log"
	//	"reflect"
	//	"strings"
	//	"swim/etc"
	"swim/serviceclient"
	//	"swim/util/orm"
	//"swim/util"
	"swim/util/sec"
)

//检查服务用户密码的正确性
func CheckServiceUser(account, pwd string) bool {
	bRet := false
	rows, err := SwDbPool.Query("select pwd from service_user where account = ?", account)
	if err != nil {
		log.Println(err)
		return bRet
	}
	defer rows.Close()

	var secPwd string

	for rows.Next() {
		err := rows.Scan(&secPwd)

		if err != nil {
			log.Fatal(err)
		} else {
			if sec.PasswdMd5(pwd) == secPwd {
				bRet = true
			}
		}

	}

	return bRet
}

//修改服务帐户的密码
func ChangeUserPwd(account, newPwd string) bool {
	bRet := true
	secPwd := sec.PasswdMd5(newPwd)
	sql := "update service_user set pwd=? where account = ?"
	_, err := SwDbPool.Exec(sql, secPwd, account)
	if err != nil {
		bRet = false
		log.Println(err.Error())
	}
	return bRet
}

//根据账号查询服务用户信息
func QueryServiceUser(account string) (serviceclient.SUser, error) {
	var user serviceclient.SUser
	rows, err := SwDbPool.Query("select id,groupid,name,levelid,account,pwd,phone,offer,last_login from service_user where account= ?",
		account)
	if err != nil {
		log.Println(err)
		return user, err
	}
	defer rows.Close()
	var (
		lastLogin sql.NullString
	)
	for rows.Next() {
		err := rows.Scan(&user.ID, &user.Group.Id, &user.Name, &user.Levelid, &user.Account,
			&user.Pwd, &user.Phone, &user.Offer, &lastLogin)

		if err != nil {
			log.Fatal(err)
			return user, err
		}
		if lastLogin.Valid {
			user.Last_login = lastLogin.String
		}
		group, gErr := QueryServiceGrooupById(user.Group.Id)
		if gErr == nil {
			user.Group = group
			//util.DeepCopy(user.Group, group)
		}

		return user, nil
	}

	return user, nil
}

func IsExistGroup(account string) bool {
	bRet := false
	rows, err := SwDbPool.Query("select name from service_group where account = ?", account)
	if err != nil {
		log.Println(err)
		return bRet
	}
	defer rows.Close()

	var name string

	for rows.Next() {
		bRet = true
		err := rows.Scan(&name)

		if err != nil {
			log.Fatal(err)
		}
	}

	return bRet
}
func QeerytGroupHint(account string) (string, bool) {
	bRet := false
	rows, err := SwDbPool.Query("select firsthint from service_group where account = ?", account)
	if err != nil {
		log.Println(err)
		return "", bRet
	}
	defer rows.Close()

	var firsthint string

	for rows.Next() {
		err := rows.Scan(&firsthint)

		if err != nil {
			log.Println(err)
		} else {
			bRet = true
		}
	}

	return firsthint, bRet
}
func QueryServiceGrooupById(groupId int) (serviceclient.GroupInfo, error) {
	var group serviceclient.GroupInfo
	rows, err := SwDbPool.Query("select id,parentid,account,levelid,name,tele,address from service_group where id= ?",
		groupId)
	if err != nil {
		log.Println(err)
		return group, err
	}
	defer rows.Close()

	for rows.Next() {
		err := rows.Scan(&group.Id, &group.ParentId, &group.Account, &group.Levelid, &group.Name,
			&group.Phone, &group.Address)

		if err != nil {
			log.Fatal(err)
			return group, err
		}

		return group, nil
	}

	return group, nil
}

func UpdateServiceUserLogin(account string) {
	var t int64 = time.Now().Unix()
	var sTime string = time.Unix(t, 0).Format("2006-01-02 15:04:05")
	sql := "update service_user set last_login=? where account=?"
	_, err := SwDbPool.Exec(sql, sTime, account)
	if err != nil {
		log.Println(err.Error())
	}
}
